from django.db.models import F
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods

from backend.models import VideoLike, PoppingVideo, VideoCollect


# 点击点赞按钮操作接口
@csrf_exempt
@require_http_methods(['GET'])
def video_like(request):
    response = {}
    try:
        vid = request.GET.get('video_id')
        username = request.GET.get('username')
        status = request.GET.get('status')
        exist_status = VideoLike.objects.filter(video_id=vid, user_name=username)
        if exist_status:
            VideoLike.objects.filter(video_id=vid, user_name=username).update(like_status=status)
            if status == 'true':
                info = PoppingVideo.objects.filter(id=vid)
                info.update(like_number=F('like_number') + 1)
                response['respCode'] = '000000'
            else:
                info = PoppingVideo.objects.filter(id=vid)
                info.update(like_number=F('like_number') - 1)
                response['respCode'] = '000000'
        else:
            VideoLike.objects.create(video_id=vid, user_name=username, like_status='true')
            info = PoppingVideo.objects.filter(id=vid)
            info.update(like_number=F('like_number') + 1)
            response['respCode'] = '000000'
    except Exception as e:
        response['respMsg'] = str(e)
    return JsonResponse(response)


# 获取用户点赞数据接口
@csrf_exempt
def video_like_status(request):
    response = {}
    try:
        vid = request.GET.get('video_id')
        username = request.GET.get('username')
        exist_status = VideoLike.objects.filter(video_id=vid, user_name=username)
        if exist_status:
            if exist_status.values()[0]['like_status'] == 'true':
                response['like_status'] = 'true'
                response['respCode'] = '000000'
            else:
                response['like_status'] = 'false'
                response['respCode'] = '000000'
        else:
            response['like_status'] = 'false'
            response['respCode'] = '000000'
    except Exception as e:
        response['respMsg'] = str(e)
        response['respCode'] = '999999'
    return JsonResponse(response)


# 获取全部点赞数据接口
@csrf_exempt
def video_like_number(request):
    response = {}
    try:
        vid = request.GET.get('video_id')
        info = PoppingVideo.objects.filter(id=vid).values()[0]['like_number']
        response['respCode'] = '000000'
        response['like_number'] = info
    except Exception as e:
        response['respMsg'] = str(e)
        response['respCode'] = '999999'
    return JsonResponse(response)


# 获取收藏状态接口
@csrf_exempt
@require_http_methods(['GET'])
def video_collect(request):
    response = {}
    try:
        vid = request.GET.get('video_id')
        username = request.GET.get('username')
        status = request.GET.get('status')
        exist_status = VideoCollect.objects.filter(video_id=vid, username=username)
        if exist_status:
            VideoCollect.objects.filter(video_id=vid, username=username).update(collect_status=status)
            if status == 'true':
                info = PoppingVideo.objects.filter(id=vid)
                info.update(collect_number=F('collect_number') + 1)
                response['respCode'] = '000000'
            else:
                info = PoppingVideo.objects.filter(id=vid)
                info.update(collect_number=F('collect_number') - 1)
                response['respCode'] = '000000'
        else:
            VideoCollect.objects.create(video_id=vid, username=username, collect_status='true')
            info = PoppingVideo.objects.filter(id=vid)
            info.update(collect_number=F('collect_number') + 1)
            response['respCode'] = '000000'
    except Exception as e:
        response['respMsg'] = str(e)
    return JsonResponse(response)


# 获取用户收藏数据接口
@csrf_exempt
def video_collect_status(request):
    response = {}
    try:
        vid = request.GET.get('video_id')
        username = request.GET.get('username')
        exist_status = VideoCollect.objects.filter(video_id=vid, username=username)
        if exist_status:
            if exist_status.values()[0]['collect_status'] == 'true':
                response['collect_status'] = 'true'
                response['respCode'] = '000000'
            else:
                response['collect_status'] = 'false'
                response['respCode'] = '000000'
        else:
            response['collect_status'] = 'false'
            response['respCode'] = '000000'
    except Exception as e:
        response['respMsg'] = str(e)
        response['respCode'] = '999999'
    return JsonResponse(response)


# 获取全部收藏数据接口
@csrf_exempt
def video_collect_number(request):
    response = {}
    try:
        vid = request.GET.get('video_id')
        info = PoppingVideo.objects.filter(id=vid).values()[0]['collect_number']
        response['respCode'] = '000000'
        response['collect_number'] = info
    except Exception as e:
        response['respMsg'] = str(e)
        response['respCode'] = '999999'
    return JsonResponse(response)